import 'dart:convert';

import 'package:flustars_flutter3/flustars_flutter3.dart';
import 'package:flutter_deer/generated/json/base/json_field.dart';
import 'package:flutter_deer/generated/json/res_zero_tube_entity.g.dart';
import 'package:flutter_deer/res/constant.dart';

@JsonSerializable()
class AppZeroModConfig {
  // int advancePayment = 0;           //提前付款 1：开启 0：关闭
  // int controlMode = 0;              //管控模式 0：强管, 1：弱管
  int mergePaymentType = 0;         //加油模式 0：同一加注枪, 1：同一能源商品
  // var orderMismatch: Int = 0,    //订单匹配误差 0：0, 1：0.5, 2：自定义
  double orderMismatchAmount = 0.0;  //订单匹配误差金额 0：0, 1：0.5, 2：自定义金额
  int refuelingMode = 0;            //加油模式 0：无卡模式, 1：插件模式, 2：开卡联动

  AppZeroModConfig();

  factory AppZeroModConfig.fromJson(Map<String, dynamic> json) => $AppZeroModConfigFromJson(json);

  Map<String, dynamic> toJson() => $AppZeroModConfigToJson(this);

  // 保存配置
  Future<void> save() async {
    await SpUtil.putObject(Constant.mAppZeroModConfig, this);
  }

  // 读取配置
  static AppZeroModConfig? getConfig() {
    final String? jsonString = SpUtil.getString(Constant.mAppZeroModConfig);
    if (jsonString?.isNotEmpty != true) {
      return null;
    }
    try {
      final dynamic data = json.decode(jsonString!);
      if (data is Map<String, dynamic>) {
        return AppZeroModConfig.fromJson(data);
      }
      return null;
    } on FormatException {
      return null;
    }
  }
}
